package kiwi.assist.dev.guieditor.view {
	import kiwi.assist.dev.guieditor.event.GUIEditorEvent;

	import org.aswing.JMenu;
	import org.aswing.JMenuBar;
	import org.aswing.JMenuItem;
	import org.aswing.JPanel;

	import flash.events.MouseEvent;

	/**
	 * 顶部的菜单
	 * @author zhangming.luo
	 */
	public class GuiEditorMenu extends JPanel {
		private var menuBar : JMenuBar;
		private var openItem : JMenuItem;
		private var saveItem : JMenuItem;
		private var publishAsCodeItem : JMenuItem;
		private var publishJsCodeItem : JMenuItem;
		private var fileMenu : JMenu;
		private var containerMenu : JMenu;
		private var selectContainerItem : JMenuItem;
		private var newItem : JMenuItem;
		private var advMenu : JMenu;
		private var batchPublishMenu : JMenuItem;

		public function GuiEditorMenu() {
			menuBar = new JMenuBar();
			menuBar.append(fileMenu = new JMenu("文件"));
			fileMenu.append(newItem = new JMenuItem("新建ui描述文件"));
			fileMenu.append(openItem = new JMenuItem("读取ui描述文件"));
			fileMenu.append(saveItem = new JMenuItem("保存ui描述文件"));
			fileMenu.append(publishAsCodeItem = new JMenuItem("导出as代码"));
			fileMenu.append(publishJsCodeItem = new JMenuItem("导出js代码"));
			append(menuBar);
			menuBar.append(containerMenu = new JMenu("容器"));
			containerMenu.append(selectContainerItem = new JMenuItem("选择容器"));
			menuBar.append(advMenu = new JMenu("高级"));
			advMenu.append(batchPublishMenu = new JMenuItem("批量导出"));
			setupEvent();
		}

		private function setupEvent() : void {
			newItem.addEventListener(MouseEvent.CLICK, handleNew);
			openItem.addEventListener(MouseEvent.CLICK, handleOpen);
			saveItem.addEventListener(MouseEvent.CLICK, handleSave);
			publishAsCodeItem.addEventListener(MouseEvent.CLICK, handlePublishAS);
			publishJsCodeItem.addEventListener(MouseEvent.CLICK, handlePublishJS);
			selectContainerItem.addEventListener(MouseEvent.CLICK, handleSelectContainer);
			batchPublishMenu.addEventListener(MouseEvent.CLICK, handleBatchPublish);
		}

		private function handleBatchPublish(event : MouseEvent) : void {
			dispatchEvent(new GUIEditorEvent(GUIEditorEvent.BATCH_PUBLISH));
		}

		private function handleNew(event : MouseEvent) : void {
			dispatchEvent(new GUIEditorEvent(GUIEditorEvent.NEW_PROJECT));
		}

		private function handleSelectContainer(event : MouseEvent) : void {
			dispatchEvent(new GUIEditorEvent(GUIEditorEvent.SHOW_CONTAINER_SELECT_DIALOG));
		}

		private function handlePublishAS(event : MouseEvent) : void {
			dispatchEvent(new GUIEditorEvent(GUIEditorEvent.PUBLISH_AS));
		}

		private function handlePublishJS(event : MouseEvent) : void {
			dispatchEvent(new GUIEditorEvent(GUIEditorEvent.PUBLISH_JS));
		}

		private function handleOpen(event : MouseEvent) : void {
			dispatchEvent(new GUIEditorEvent(GUIEditorEvent.LOAD_UIXML));
		}

		private function handleSave(event : MouseEvent) : void {
			dispatchEvent(new GUIEditorEvent(GUIEditorEvent.SAVE_UIXML));
		}
	}
}
